/*
 * @Description: promise.all使用
 * @Author: Zhanghan
 * @Date: 2025-03-24 09:20:40
 * @LastEditTime: 2025-03-24 09:51:12
 * @LastEditors: Zhanghan
 */
import { useState } from "react";
import { Button } from "antd";
function All() {
  const [flag, setFlag] = useState<boolean>(true);
  const [str, setStr] = useState<string>();
  const handleAll = async () => {
    const promise1 = Promise.resolve(3);
    const promise2 = 42;
    const promise3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        if (flag) {
        return  resolve("返回失败");
        } else {
         return reject("返回失败");
        }
      }, 1000);
    });
    try {
      const res = await Promise.all([promise1, promise2, promise3]);
      console.log("🚀 ~ handleAll ~ res:", res);
      setStr('成功')
    } catch (err) {
      console.log("🚀 ~ handleAll ~ err:", err);
      setStr('失败')
    }
  };
  return (
    <>
      <div className="mb-2">
        promise.all 只要一个失败就返回失败状态，全部成功才是成功状态++{ str ?? '点击执行'}
      </div>
      <div className="flex items-center">
        <Button
          type="primary"
          className="mr-2"
          onClick={() => {
            setFlag(!flag);
          }}
        >
          设置返回状态
        </Button>
        <Button type="primary" onClick={handleAll}>
          点击执行all
        </Button>
      </div>
    </>
  );
}

export default All;
